package com.ruoyi.web.controller.official;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.official.domain.OfficialCategory;
import com.ruoyi.official.service.IOfficialCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分类Controller
 *
 * @author yuanqiji
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/official/category")
public class OfficialCategoryController extends BaseController {
    @Autowired
    private IOfficialCategoryService officialCategoryService;

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('official:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(OfficialCategory officialCategory) {
        List<OfficialCategory> list = officialCategoryService.selectOfficialCategoryList(officialCategory);
        return getDataTable(list);
    }

    /**
     * 查询分类列表
     */
    @PreAuthorize("@ss.hasPermi('official:category:list')")
    @GetMapping("/all")
    public AjaxResult all(OfficialCategory officialCategory) {
        return AjaxResult.success(officialCategoryService.selectOfficialCategoryList(officialCategory));

    }

    /**
     * 导出分类列表
     */
    @PreAuthorize("@ss.hasPermi('official:category:export')")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OfficialCategory officialCategory) {
        List<OfficialCategory> list = officialCategoryService.selectOfficialCategoryList(officialCategory);
        ExcelUtil<OfficialCategory> util = new ExcelUtil<OfficialCategory>(OfficialCategory.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('official:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(officialCategoryService.selectOfficialCategoryById(id));
    }

    /**
     * 新增分类
     */
    @PreAuthorize("@ss.hasPermi('official:category:add')")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OfficialCategory officialCategory) {
        return toAjax(officialCategoryService.insertOfficialCategory(officialCategory));
    }

    /**
     * 修改分类
     */
    @PreAuthorize("@ss.hasPermi('official:category:edit')")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OfficialCategory officialCategory) {
        return toAjax(officialCategoryService.updateOfficialCategory(officialCategory));
    }

    /**
     * 删除分类
     */
    @PreAuthorize("@ss.hasPermi('official:category:remove')")
    @Log(title = "分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (officialCategoryService.selectOfficialProductList(ids) > 0) {
            return AjaxResult.warn("分类下存在产品，无法删除");
        }
        return toAjax(officialCategoryService.deleteOfficialCategoryByIds(ids));
    }
}
